//
//  TGMActivityIndicatorView.m
//  TravelGuideMdd
//
//  Created by Song Xiaofeng on 13-6-24.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "TGMActivityIndicatorView.h"
#import "YYAnimatedImageView.h"
#import "YYImage.h"

#define _ANIMATION_DURATION 0.2

#define _TICK_INTERVAL  (_ANIMATION_DURATION + 0.2)

#define _INDICATOR_SIZE     5
#define _INDICATOR_SPACING  2

@interface TGMActivityIndicatorView()
{
    BOOL _setAnimating;
    
    NSUInteger _animatingTickIndex;
    
    NSMutableArray *_indicatorViewsArray;
    
    CGFloat alphaFactor;
    UIImageView *_indicatorIv;
    YYAnimatedImageView * _animationImageView;
}
@property(nonatomic, strong)NSTimer *timer;
@end

@implementation TGMActivityIndicatorView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self _indicatorInitialize];
    }
    return self;
}


- (void)awakeFromNib
{
    [self _indicatorInitialize];
}

- (CGSize)sizeThatFits:(CGSize)size;
{
    CGFloat si = _INDICATOR_SIZE + _INDICATOR_SIZE + _INDICATOR_SPACING;
    return CGSizeMake(si, si);
}

- (void)_indicatorInitialize
{
    _setAnimating = YES;
    _isAnimating = NO;
    _animatingTickIndex = 0;
    _hidesWhenStopped = NO;
    
    self.backgroundColor = [UIColor clearColor];
    
    
    
//    int startX = 0;
//    int startY = 0;
    
    YYImage *image = [YYImage imageNamed:@"loding"];
    _animationImageView = [[YYAnimatedImageView alloc]initWithImage:image];
    _animationImageView.size = CGSizeMake(65, 65);
    _animationImageView.center = self.center;
//    _indicatorIv = [[UIImageView alloc] initWithFrame:CGRectMake(startX, startY, 25, 27.5)];
//    NSArray *imgs = [NSArray arrayWithObjects:[UIImage imageNamed:@"laoding_mogu_1.png"], [UIImage imageNamed:@"loading_mogu_2.png"], nil];
//    _indicatorIv.animationImages = imgs;
//    _indicatorIv.image = [UIImage imageNamed:@"laoding_mogu_1"];
//    _indicatorIv.animationDuration = 0.3;
    [self addSubview:_animationImageView];
    
    [self _restAnimationInitialStatus];
}


- (void)_restAnimationInitialStatus
{

}

- (void)dealloc
{
//    DLog(@"&&&&&&&&　dealloc");
}


- (void)_updateAnimatingStatus
{
    
}

- (void)_animatingTick
{
    
}

- (void)setColor:(UIColor *)color
{
    
}


- (void)_startTimer
{
    
}

- (void)_stopTimer;
{
    
}

- (void)startAnimating;
{
    if ([self superview] == nil) return;
    
    _setAnimating = YES;
    if(![_animationImageView isAnimating]) {
        [_animationImageView startAnimating];
    }
}

- (void)stopAnimating;
{
    if ([self superview] == nil) return;
    
    _setAnimating = NO;
    if([_animationImageView isAnimating]) {
        [_animationImageView stopAnimating];
    }
}

- (BOOL)isAnimating;
{
    return [_animationImageView isAnimating];
}

+ (TGMActivityIndicatorView*)defaultActivityIndicatorView;
{
    return [[self alloc] initWithFrame:CGRectMake(0, 0, 25, 27)];
}

#pragma mark exexuteAnimation
//add by liuche at 2014 06-19
- (void)startLoadingAtFatherView:(UIView *)fatherView
{
    if (self == nil && fatherView != nil)
    {
        return;
    }
    [fatherView addSubview:self];
    self.center = CGPointMake(kScreenWidth/2,kScreenWidth/2);
    [self startAnimating];
}
- (void)stopLoading
{
    if( self != nil){
        [self stopAnimating];
        [self removeFromSuperview];
    }
}



@end
